﻿using ConsoleApplication.WebAPI;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HM
{
    [HotUpdate]
    [Description("聚合账核销结果，直接调拨单自动下推销售出库单(换货)")]
    public class HHPushSalOut : IScheduleService
    {
        public void Run(Context ctx, Schedule schedule)
        {
            try
            {
                var result = InvokeHelper.Login();
                var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
                if (iResult == 1)
                {
                    string F_RXWU_Text6 = "", FEntryId = "", FBILLNO = "", F_RXWU_Date2 = "";
                    string SQL = "/*dialect*/" + string.Format(@"select t1.F_RXWU_Date2,t1.FEntryId, t1.F_RXWU_Text6 from RXWU_t_Cust100002 t
                    left join RXWU_t_Cust_Entry100013 t1 on t.fid=t1.fid
                    where t1.F_RXWU_Text7 like '%补发%'
                    AND T1.F_RXWU_Date2>'2023-08-01'
                    ORDER BY T1.F_RXWU_Date2
                    ");
                    DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(ctx, SQL);
                    if (DynamData.Count > 0)
                    {
                        for (int i = 0; i < DynamData.Count; i++)
                        {
                            F_RXWU_Date2 = Convert.ToString(DynamData[i]["F_RXWU_Date2"]);
                            F_RXWU_Text6 = Convert.ToString(DynamData[i]["F_RXWU_Text6"]);
                            FEntryId = Convert.ToString(DynamData[i]["FEntryId"]);
                            string SQL0 = "/*dialect*/" + string.Format(@"select FBILLNO from T_STK_STKTRANSFERIN where F_RXWU_Text2 ='{0}'", F_RXWU_Text6);
                            DynamicObjectCollection DynamData0 = DBUtils.ExecuteDynamicObject(ctx, SQL0);
                            if (DynamData0.Count > 0)
                            {
                                for (int k = 0; k<DynamData0.Count; k++)
                                {
                                    FBILLNO = Convert.ToString(DynamData0[k]["FBILLNO"]);
                                    string json = "{\"Ids\":\"\",\"Numbers\":['" + FBILLNO + "'],\"EntryIds\":\"\",\"RuleId\":\"TransferDirect-OUTSTOCK\",\"TargetBillTypeId\":\"\",\"TargetOrgId\":0,\"TargetFormId\":\"SAL_OUTSTOCK\",\"IsEnableDefaultRule\":\"false\",\"IsDraftWhenSaveFail\":\"false\",\"CustomParams\":{}}";
                                    result = InvokeHelper.Push("STK_TransferDirect", json.ToString());
                                    JObject jobject = JObject.Parse(result);
                                    string a = Convert.ToString(jobject["Result"]["ResponseStatus"]["IsSuccess"]);
                                    bool IsSuccess = a == "True";
                                    if (IsSuccess)
                                    {
                                        string SQL1 = "/*dialect*/" + string.Format(@"update RXWU_t_Cust_Entry100013 set F_RXWU_COMBO='1' where FEntryId='{0}'", FEntryId);
                                        DBUtils.Execute(ctx, SQL1);
                                        Log.WriteLog("直接调拨单自动下推销售出库单(换货)成功", "报文:" + json.ToString() + "/r/n" + "返回结果:" + result.ToString());
                                        
                                        var SuccessEntitysId = jobject["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                                        string SQL2 = "/*dialect*/" + string.Format(@"update T_SAL_OUTSTOCK set FDATE='{1}' where FID='{0}'", SuccessEntitysId, F_RXWU_Date2);
                                        DBUtils.Execute(ctx, SQL2);
                                        string Submit = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" + SuccessEntitysId + "\",\"SelectedPostId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                        result = InvokeHelper.Submit("SAL_OUTSTOCK", Submit);
                                        string Audit = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" + SuccessEntitysId + "\",\"InterationFlags\":\"\",\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                        result = InvokeHelper.Audit("SAL_OUTSTOCK", Audit);
                                    }
                                    else
                                    {
                                        Log.WriteLog("直接调拨单自动下推销售出库单(换货)失败", "报文:" + json.ToString() + "/r/n" + "返回结果:" + result.ToString());
                                        string SQL1 = "/*dialect*/" + string.Format(@"update RXWU_t_Cust_Entry100013 set F_RXWU_COMBO='0' where FEntryId='{0}'", FEntryId);
                                        DBUtils.Execute(ctx, SQL1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("直接调拨单自动下推销售出库单(换货)程序报错", "原因:" + ex.ToString());
            }
        }
    }
}
